# This R code can be used for the replication of Figure B1 in the paper.
# See https://synth-inference.github.io/synthdid for details of Synthetic DID.


## Package and Data Load

# remotes::install_github("synth-inference/synthdid")
library(tidyverse)
library(synthdid)
library(ggpubr)

HEIs <- read.csv("Synthetic Control.csv")



## Synthetic DID Based on High Centralized IT Group

HEIs_central_IT <- HEIs %>% 
  select(IPEDCode, year_quarter, student_rating, high_central_IT_group) %>% 
  mutate(treated = if_else(high_central_IT_group==1 & (year_quarter==14L | year_quarter==15L), 1, 0)) %>% 
  select(-high_central_IT_group)

setup = panel.matrices(HEIs_central_IT)
tau.hat_central_IT = synthdid_estimate(setup$Y, setup$N0, setup$T0)
figure_central_IT <- plot(tau.hat_central_IT) + 
  scale_x_continuous(breaks=seq(1,15,by=1), labels=c("1" = "2017 Q1", "2" = "2017 Q2", "3" = "2017 Q3", "4" = "2017 Q4", 
                                                     "5" = "2018 Q1", "6" = "2018 Q2", "7" = "2018 Q3", "8" = "2018 Q4", 
                                                     "9" = "2019 Q1", "10" = "2019 Q2", "11" = "2019 Q3", "12" = "2019 Q4", 
                                                     "13" = "2020 Q1", "14" = "2020 Q2", "15" = "2020 Q3")) +
  xlab(NULL) + ylab("Student Ratings") +
  theme(axis.text.x = element_text(angle = 90, size = 10), axis.text.y = element_text(size = 10)) +
  ylim(3.7,4.2)



## Synthetic DID Based on High Decentralized IT Group

HEIs_decentral_IT <- HEIs %>% 
  select(IPEDCode, year_quarter, student_rating, high_decentral_IT_group) %>% 
  mutate(treated = if_else(high_decentral_IT_group==1 & (year_quarter==14L | year_quarter==15L), 1, 0)) %>% 
  select(-high_decentral_IT_group)

setup = panel.matrices(HEIs_decentral_IT)
tau.hat_decentral_IT = synthdid_estimate(setup$Y, setup$N0, setup$T0)
figure_decentral_IT <- plot(tau.hat_decentral_IT) + 
  scale_x_continuous(breaks=seq(1,15,by=1), labels=c("1" = "2017 Q1", "2" = "2017 Q2", "3" = "2017 Q3", "4" = "2017 Q4", 
                                                     "5" = "2018 Q1", "6" = "2018 Q2", "7" = "2018 Q3", "8" = "2018 Q4", 
                                                     "9" = "2019 Q1", "10" = "2019 Q2", "11" = "2019 Q3", "12" = "2019 Q4", 
                                                     "13" = "2020 Q1", "14" = "2020 Q2", "15" = "2020 Q3")) +
  theme(axis.text.x = element_text(angle = 90, size = 10), axis.text.y = element_text(size = 10)) +
  xlab(NULL) + ylab("Student Ratings") + 
  ylim(3.7,4.2)



## Visualize the Results

ggarrange(figure_central_IT, figure_decentral_IT)

ggsave("Tables and Figures/figure_b1.png", width = 8, height = 4)

